package org.dut.book.Controller;

import jakarta.servlet.http.HttpSession;
import org.dut.book.Constants.Constants;
import org.dut.book.Service.UserService;
import org.dut.book.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public String login(String userName, String password, HttpSession session) {
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)) {
            return "账号或密码为空";
        }
        //根据用户名和密码查询数据库
        //查询到返回用户信息
        UserInfo userInfo = userService.getUserInfoByNameAndPassword(userName,password);
        if(userInfo==null) {
            return "用户不存在或者密码错误";
        }

        session.setAttribute(Constants.USER_INFO, userInfo);
        return "";
    }
}
